package com.gisuni.sddc.platformManager.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONObject;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Record;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.gisuni.sddc.platformManager.model.Organization;
import com.gisuni.sddc.util.StringUtil;

@IocBean
public class OrganizationDao {
	@Inject
	private Dao gisplatform;  
	
    public Boolean updateUserOrganization(String updateSql ){
    	
    	Boolean result = false;
    	Sql sql = Sqls.create(updateSql);
	    sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				return true;
			}
		});
	    gisplatform.execute(sql);
	    return  sql.getObject(Boolean.class );
 
    }
  
	/**
	 * 查询用户权限
	 * @param selectSql
	 * @return
	 */
	public String queryUser(String  selectSql) {
		Sql sql = Sqls.create(selectSql);
	    sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				JSONArray result=new JSONArray();
				int numberOfColumns = rs.getMetaData().getColumnCount();
				int rows = 0;
			     while (rs.next()){
			    	 JSONObject ret=new JSONObject();
			    	 rows++;
						// 遍历查询结果字段
						for (int i = 0; i < numberOfColumns; i++) {
							Object obj = rs.getString(i + 1);
							ret.put(rs.getMetaData().getColumnName(i+1).trim(), obj);
						}
			    	 result.put(ret);
			     }
			     JSONObject ret=new JSONObject();
			     ret.put("total", rows);
			     result.put(ret);
				return result.toString();
			}
		});
	    gisplatform.execute(sql);
	    return  sql.getObject(String.class);
	}
	
	public String getControlRegion(String roleName){
		String result = "";
		if(!StringUtil.isNullOrEmpty(roleName)){
			 
			
		}
		return result;
	}


	public List<Organization> findOrganizationAllByPidNull() {
		
		Sql sql = Sqls.create("select t.id,t.parent_id,t.organization_name,t.organization_type from SDDCGISPLATFORM.organization t");
	    sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				List<Organization> result = new ArrayList<Organization>();
			     while (rs.next()){
			    	 Organization organization=new Organization();
			    	 organization.setId(rs.getString("ID"));
			    	 organization.setParentId(rs.getString("PARENT_ID"));
			    	 organization.setOrganizationName(rs.getString("ORGANIZATION_NAME"));
			    	 organization.setOrganizationType(rs.getString("ORGANIZATION_TYPE"));
			    	 result.add(organization);
			     }
			     
				return result;
			}
		});
	    gisplatform.execute(sql);
	    return  sql.getObject(List.class);
	}


	public List<Organization> findOrganizationAll(String selectSql) {
		Sql sql = Sqls.create(selectSql);
	    sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				List<Organization> result = new ArrayList<Organization>();
			     while (rs.next()){
			    	 Organization organization=new Organization();
			    	 organization.setId(rs.getString("ID"));
			    	 organization.setParentId(rs.getString("PARENT_ID"));
			    	 organization.setOrganizationName(rs.getString("ORGANIZATION_NAME"));
			    	 organization.setOrganizationType(rs.getString("ORGANIZATION_TYPE"));
			    	 try{
				    	 if(rs.findColumn("MENAGER_NAME")>0){
				    		 organization.setManagerName(rs.getString("MENAGER_NAME"));
				    	 }
			    	 }catch(Exception e){
			    		 
			    	 }
			    	 result.add(organization);
			     }
			     
				return result;
			}
		});
	    gisplatform.execute(sql);
	    return  sql.getObject(List.class);
//		return null;
	}
	

	public List<Organization> queryOrganizationAll(String selectSql) {
		Sql sql = Sqls.create(selectSql);
	    sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				List<Organization> result = new ArrayList<Organization>();
				Map<String,Organization> map = new HashMap<String,Organization>();
			     while (rs.next()){
			    	 Organization organization=new Organization();
			    	 String orgId = rs.getString("ID");
			    	 String orgType = rs.getString("ORGANIZATION_TYPE");
			    	 //如果存在则取出来
			    	 if(map.get(orgId)!=null){
			    		 organization = map.get(orgId);
			    	 }else{
			    		 organization.setId(orgId);
			    		 organization.setParentId(rs.getString("PARENT_ID"));
				    	 organization.setOrganizationName(rs.getString("ORGANIZATION_NAME"));
				    	 organization.setOrganizationType(orgType);
				    	 
				    	 try{
					    	 if(rs.findColumn("MENAGER_NAME")>0){ //负责人
					    		 organization.setManagerName(rs.getString("MENAGER_NAME"));
					    	 }
				    	 }catch(Exception e){
				    		 
				    	 }
			    	 }
			    	 
			    	 if("1".equals(orgType)){
				    	 try{
					    	   //集团拓展阶段类型
					    		 String wytzType = rs.getString("COMPANYWYTZTYPE");
					    		 String target = rs.getString("COMPANYTARGET");//集团指标
						    	 if("1".equals(wytzType)){
						    		 organization.setHechaTarget(target);
						    	 }else if("2".equals(wytzType)){
						    		 organization.setQiatanTarget(target);
						    	 }
					    	 
				    	 }catch(Exception e){
				    		 
				    	 }
	 
			    	 }

			    	 if("2".equals(orgType)){
				    	 try{
				    	   //事业部拓展阶段类型
				    		 String wytzType = rs.getString("BUSINESSWYTZTYPE");
				    		 String target = rs.getString("BUSINESSTARGET");//事业部指标
					    	 if("1".equals(wytzType)){
					    		 organization.setHechaTarget(target);
					    	 }else if("2".equals(wytzType)){
					    		 organization.setQiatanTarget(target);
					    	 }
				    	 }catch(Exception e){
				    		 
				    	 }
			    		 
			    	 }

			    	 if("3".equals(orgType)){
				    	 try{
					    	  //拓展组拓展阶段类型
				    		 String wytzType = rs.getString("DEVELOPWYTZTYPE");
				    		 String target = rs.getString("DEVELOPTARGET");//拓展组指标
					    	 if("1".equals(wytzType)){
					    		 organization.setHechaTarget(target);
					    	 }else if("2".equals(wytzType)){
					    		 organization.setQiatanTarget(target);
					    	 }
				    	 }catch(Exception e){
				    		 
				    	 }
			    	 }
			    	 
			    	 map.put(orgId, organization);
			    	 
			     }
			     
			     //对Map进行取值
			     Set<String> set =  map.keySet();
			     Iterator<String> iterator = set.iterator();
			     while(iterator.hasNext()){
			    	 result.add(map.get(iterator.next()));
			     }
			     
				return result;
			}
		});
	    gisplatform.execute(sql);
	    return  sql.getObject(List.class);
//		return null;
	}
	


	public boolean addOrganization(Organization org) {
		Organization t = gisplatform.insert(org);
		if(t!=null){
			return true;
		}
		return false;
	}




	public Organization findOrganizationById(String id) {
		Organization org = new Organization();
		org.setId(id);
//		gisplatform.getEntity(org)
//		List<Record>  re  = gisplatform.query("ORGANIZATION", Cnd.where("ID","=",id));
		Organization res = gisplatform.fetch(Organization.class,id );
 
		return res;
	}



	public boolean updateOrganization(Organization org) {
		if(gisplatform.updateIgnoreNull(org)>0)
			return true;
		return false;
	}


	public int delOrganization(Organization org) {
		int i=1;//操作成功
		
		//判断是否有下级组织
		List<Record> record = gisplatform.query("ORGANIZATION", Cnd.where("PARENT_ID","=",org.getId()));
		
		if(record!=null && record.size()>0){
			 i=2; //有下级组织
		}
		
		int res = gisplatform.delete(org);
		if(res<0){
			 i=0;//删除失败
		}
		return i;
	}
	
 
    public Boolean executeSql(String updateSql ){
    	
    	Sql sql = Sqls.create(updateSql);
	    sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				return true;
			}
		});
	    gisplatform.execute(sql);
	    return  sql.getObject(Boolean.class );
 
    }
	
}
